package com.fulin.leetcode;

/**
 * 整数数组加一
 *
 * @Author fulin
 * @Date 2022/9/16 13:51
 **/
public class Num66 {
    public int[] plusOne(int[] digits) {
        int len = digits.length;
        if(len < 1) return digits;
        int up = 0;
        int i = len - 1;
        for(; i >= 0; i--){
            int cur = digits[i];
            if(i == len - 1){
                cur = cur + 1 + up;
            }else{
                cur = cur + up;
            }
            up = cur / 10;
            cur = cur % 10;
            digits[i] = cur;
            if(up < 1){
                break;
            }
        }

        if(i < 0 && up > 0){
            int[] result = new int[len + 1];
            for(int j = 0; j < len; j++){
                result[j + 1] = digits[j];
            }
            result[0] = 1;
            return result;
        }

        return digits;
    }
}
